$(function(){
    $(".clsAddress").on('change',function(index,item){
        var otype = $(this).attr("data-value");
        var parentId = $(this).val();
        $.ajax({
            url : "/ajaxAdd",
            type : "POST",
            async:false,
            data : {otype:otype,parentId:parentId},
            dataType : "json",
            success : function(data) {
                $.each(data,function(key,values){
                    console.log(key);
                    if(key=="city"){
                        var cityObj = $("#o_city");
                        cityObj.empty();
                        var cityHtml = "";
                        $(values).each(function(index,item){
                            var dataObj = JSON.stringify(item);
                            var jsonObject = JSON.parse(dataObj);
                            cityHtml += "<option value='"+jsonObject.id+"'>";
                            cityHtml += jsonObject.name;
                            cityHtml += "</option>";
                            console.log("index"+index+"item"+jsonObject);
                        });
                        console.log("cityHtml"+cityHtml);
                        cityObj.html(cityHtml);
                    }else if(key="location"){
                        var locationObj = $("#o_location");
                        locationObj.empty();
                        var locationHtml = "";
                        $(values).each(function(index,item){
                            var dataObj = JSON.stringify(item);
                            var jsonObject = JSON.parse(dataObj);
                            locationHtml += "<option value='"+jsonObject.id+"'>";
                            locationHtml += jsonObject.name;
                            locationHtml += "</option>";
                            console.log("index"+index+"item"+dataObj);
                        });
                        locationObj.html(locationHtml);
                    }
                });
            }
        });
    });


    $("#nick_button").on('click',function(){
        var newNick = $("#newNick").val();
        if(newNick=="" || newNick==null){
            layer.msg("数据不能为空");
            return false;
        };
        $.ajax({
            url : "/user/profile/nick",
            type : "POST",
            data : {newNick:newNick},
            dataType : "json",
            success : function(data) {
                layer.msg('修改成功，跳转中...', {
                    icon: 1,
                    time: 2000 //2秒关闭（如果不配置，默认是3秒）
                }, function(){
                    window.location.href = "/user/profile";
                });
            }
        });
    });

    $("#address_button").on('click',function(){
        var oprovince = $("#o_province").val();
        if(oprovince=="" || oprovince==null){
            layer.msg("数据不能为空");
            return false;
        };
        var ocity = $("#o_city").val();
        if(ocity=="" || ocity==null){
            layer.msg("数据不能为空");
            return false;
        };
        var olocation = $("#o_location").val();
        if(olocation=="" || olocation==null){
            layer.msg("数据不能为空");
            return false;
        };

        $.ajax({
            url : "/user/profile/address",
            type : "POST",
            data : {oprovince:oprovince,ocity:ocity,olocation:olocation},
            dataType : "json",
            success : function(data) {
                layer.msg('修改成功，跳转中...', {
                    icon: 1,
                    time: 2000 //2秒关闭（如果不配置，默认是3秒）
                }, function(){
                    window.location.href = "/user/profile";
                });
            }
        });
    });

    $("#mobile_button").on('click',function(){
        var newMobile = $("#newMobile").val();
        if(newMobile=="" || newMobile==null){
            layer.msg("数据不能为空");
            return false;
        };
        $.ajax({
            url : "/user/profile/mobile",
            type : "POST",
            data : {newMobile:newMobile},
            dataType : "json",
            success : function(data) {
                if(data.stats == "200"){
                    layer.msg('修改成功，跳转中...', {
                        icon: 1,
                        time: 2000 //2秒关闭（如果不配置，默认是3秒）
                    }, function(){
                        window.location.href = "/user/profile";
                    });
                }else{
                    layer.msg(data.messages, {
                        icon: 2,
                        time: 2000 //2秒关闭（如果不配置，默认是3秒）
                    }, function(){
                    });
                }
            }
        });
    });

});